//
//  SYYTabBarController.m
//  NineteenBook
//
//  Created by xhkj on 2021/8/2.
//  Copyright © 2021 58阅读. All rights reserved.
//

#import "SYYTabBarController.h"

#import "SYYBaseVC.h"
#import "SYYBaseNavVC.h"


@interface SYYTabBarController ()
@property (nonatomic,weak)UITabBarItem *lastItem;
@end

@implementation SYYTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
   
}


- (void)setupSubViewControllers
{
//    NSArray *vcsArray = @[@"UPHomeVC",@"UPCallPhoneVC",@"UPUserVC"];
//    NSArray *titlesArray = @[@"首页",@"通话",@"我的"];
//    NSArray *norImgsArray = @[@"ic_tab_home_unselect",@"ic_tab_call_unselect",@"ic_tab_my_select"];
//    NSArray *selImgsArray = @[@"ic_tab_home_select",@"ic_tab_call_select",@"ic_tab_my_unselect"];
    
    NSArray *vcsArray = self.vcsArray;
    NSArray *titlesArray = self.titlesArray;
    NSArray *norImgsArray = self.norImgsArray;
    NSArray *selImgsArray = self.selImgsArray;
    
    // 添加子控制器
    for (int i = 0; i < vcsArray.count; i++) {
        [self addChildVc:[[NSClassFromString(vcsArray[i]) alloc] init] title:titlesArray[i] image:HHIMAGE(norImgsArray[i]) selectedImage:HHIMAGE(selImgsArray[i])];
    }
    
    // 去掉黑线
    self.tabBar.backgroundImage = [UIImage new];
    self.tabBar.shadowImage = [UIImage new];
 
    if (@available(iOS 10.0, *)) {//ios13这样设置颜色才有用
        [self.tabBar setUnselectedItemTintColor:HHColorHex000000];
    }
    
    //背景颜色
    UIView *tabBg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, TabBarHeight + BottomSafeHeight)];
    [self.tabBar addSubview:tabBg];
    tabBg.backgroundColor = HHColorHexFFFFFF;
    
    [self addShadowLineMethod];
}



- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    if (self.tabBar.height < 30) {
        
        self.tabBar.height = StatusBarHeight > 20? 83:49;
    }
}

- (void)addShadowLineMethod{
    UIView *shadowLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, TabBarHeight + BottomSafeHeight)];
    [self.tabBar addSubview:shadowLine];
//    shadowLine.backgroundColor = HHColor();
    shadowLine.backgroundColor = HHColorHexFFFFFF;
    shadowLine.layer.shadowColor = [HHColorHex(0xEEEEEE) CGColor];
    shadowLine.layer.shadowOpacity = 0.5f;
    shadowLine.layer.shadowRadius = 5.f;
    shadowLine.layer.shadowOffset = CGSizeMake(0,0);
}



/**
 *  添加一个子控制器
 *
 *  @param childVc       子控制器
 *  @param title         标题
 *  @param image         图片
 *  @param selectedImage 选中的图片
 */
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(UIImage *)image selectedImage:(UIImage *)selectedImage {
    // 设置子控制器的文字(可以设置tabBar和navigationBar的文字)
    childVc.title = title;
    
    // 设置子控制器的tabBarItem图片
    childVc.tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 禁用图片渲染
    childVc.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 设置文字的样式,字体大小
    [childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : self.norFontColor?self.norFontColor:HHColorHex000000,NSFontAttributeName:HHBoldFont(10)} forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : self.selFontColor?self.selFontColor:HHColorMain,NSFontAttributeName:HHBoldFont(10)} forState:UIControlStateSelected];
 
    
    // 为子控制器包装导航控制器
    SYYBaseNavVC *navigationVc = [[SYYBaseNavVC alloc]initWithRootViewController:childVc];
    // 添加子控制器
    [self addChildViewController:navigationVc];
}



- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    self.lastItem = item;
}

@end
